package com.lion.appletrip;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.geocode.GeoCodeResult;
import com.baidu.mapapi.search.geocode.GeoCoder;
import com.baidu.mapapi.search.geocode.OnGetGeoCoderResultListener;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeOption;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeResult;
import com.lion.appletrip.model.fg2location.AllCityList;
import com.lion.appletrip.model.fg2location.CityData;
import com.lion.appletrip.model.fg2location.DomesticCity;
import com.lion.appletrip.model.fg2location.LocationData;
import com.lion.appletrip.model.fg2location.OverseaCity;
import com.lion.appletrip.utils.CustomerGridView;
import com.xinbo.utils.GsonUtils;

public class CitySelectActivity extends Activity implements
        OnGetGeoCoderResultListener
{
	private LocationClient mLocClient;
	public MyLocationListenner myListener = new MyLocationListenner();
	GeoCoder mSearch = null;
	private View btnCountry;
	private View btnOver;
	private List<String> hotCityList = new ArrayList<String>();
	private List<AllCityList> allCityList = new ArrayList<AllCityList>();
	private View headview;
	private GridAdapter gridAdapter;
	private ListAdapter listAdapter;
	private DomesticCity domesticCity;
	private OverseaCity overseaCity;
	private TextView tvCityName;
	private String cityName;
	private CustomerGridView mCustomerGridView;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_city_select);

		// 百度定位初始化操作
		mLocClient = new LocationClient(this);
		mLocClient.registerLocationListener(myListener);
		LocationClientOption option = new LocationClientOption();
		option.setOpenGps(true); // 打开gps
		option.setCoorType("bd09ll"); // 设置坐标类型
		option.setScanSpan(1000);
		mLocClient.setLocOption(option);
		mLocClient.start();
		mSearch = GeoCoder.newInstance();
		mSearch.setOnGetGeoCodeResultListener(this);

		Intent intent = getIntent();
		String json = intent.getStringExtra("cityData");
		LocationData locationData = GsonUtils.parseJSON(json,
		        LocationData.class);
		CityData cityData = locationData.getCityData();
		overseaCity = cityData.getOverseaCity();
		domesticCity = cityData.getDomesticCity();
		hotCityList = domesticCity.getHotCityList();
		allCityList = domesticCity.getAllCityList();
		btnCountry = findViewById(R.id.btn_country_cityselect);
		btnOver = findViewById(R.id.btn_overcountry_cityselect);
		gridAdapter = new GridAdapter();
		listAdapter = new ListAdapter();
		ListView mListView = (ListView) findViewById(R.id.listView1_cityselect);
		initHeadview();
		mListView.addHeaderView(headview, null, false);
		mListView.setAdapter(listAdapter);
		btnCountry.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{

				btnCountry.setBackgroundResource(R.drawable.bg_textview);
				btnOver.setBackgroundResource(R.drawable.bg_textview2);
				hotCityList = domesticCity.getHotCityList();
				allCityList = domesticCity.getAllCityList();
				gridAdapter.notifyDataSetChanged();
				listAdapter.notifyDataSetChanged();
			}
		});
		btnOver.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				btnCountry.setBackgroundResource(R.drawable.bg_textview2);
				btnOver.setBackgroundResource(R.drawable.bg_textview);
				hotCityList = overseaCity.getHotCityList();
				allCityList = overseaCity.getAllCityList();
				gridAdapter.notifyDataSetChanged();
				listAdapter.notifyDataSetChanged();
			}
		});
		tvCityName.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent();
				intent.putExtra("CityName", cityName);
				intent.putExtra("Login", 1);
				intent.setClass(CitySelectActivity.this, MainActivity.class);
				setResult(0, intent);
				finish();
			}
		});
		mCustomerGridView.setOnItemClickListener(new OnItemClickListener()
		{

			@Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id)
            {
	            String cityNameg = hotCityList.get(position);
	            Intent intent = new Intent();
				intent.putExtra("CityName", cityNameg);
				intent.putExtra("Login", 1);
				intent.setClass(CitySelectActivity.this, MainActivity.class);
				setResult(0, intent);
				finish();
            }
		});
		mListView.setOnItemClickListener(new OnItemClickListener()
		{

			@Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id)
            {
				position-=1;
	            AllCityList allCityList2 = allCityList.get(position);
	            String cityNamel = allCityList2.getName();
	            Intent intent = new Intent();
				intent.putExtra("CityName", cityNamel);
				intent.putExtra("Login", 1);
				intent.setClass(CitySelectActivity.this, MainActivity.class);
				setResult(0, intent);
				finish();
            }
		});
	}

	private void initHeadview()
	{
		headview = getLayoutInflater().inflate(
		        R.layout.activity_city_select_headview, null);
		tvCityName = (TextView) headview.findViewById(R.id.tv_name_location);
		mCustomerGridView = (CustomerGridView) headview
		        .findViewById(R.id.customerGridView1_cityselect);
		mCustomerGridView.setAdapter(gridAdapter);
	}

	class GridAdapter extends BaseAdapter
	{

		@Override
		public int getCount()
		{

			return hotCityList.size();
		}

		@Override
		public Object getItem(int position)
		{
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position)
		{
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent)
		{
			View layout = getLayoutInflater().inflate(
			        R.layout.fragment_fragment1_popuwindows_item, null);
			String cityName = hotCityList.get(position);
			TextView mTextView = (TextView) layout
			        .findViewById(R.id.tv_popuwindows);
			mTextView.setText(cityName);
			return layout;
		}

	}

	class ListAdapter extends BaseAdapter
	{

		@Override
		public int getCount()
		{
			// TODO Auto-generated method stub
			return allCityList.size();
		}

		@Override
		public Object getItem(int position)
		{
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position)
		{
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent)
		{
			View layout = getLayoutInflater().inflate(
			        R.layout.activity_city_select_listview_item, null);
			TextView mTextView = (TextView) layout
			        .findViewById(R.id.tv_name_listview_item_cityselect);
			AllCityList allCityList2 = allCityList.get(position);
			mTextView.setText(allCityList2.getName());
			return layout;
		}

	}

	/**************************** 百度地位解码 *************************/

	/**
	 * 定位SDK监听函数
	 */
	public class MyLocationListenner implements BDLocationListener
	{

		@Override
		public void onReceiveLocation(BDLocation location)
		{
			LatLng ll = new LatLng(location.getLatitude(),
			        location.getLongitude());
			mSearch.reverseGeoCode(new ReverseGeoCodeOption().location(ll));

		}

		public void onReceivePoi(BDLocation poiLocation)
		{
		}
	}

	@Override
	public void onGetGeoCodeResult(GeoCodeResult arg0)
	{
	}

	@Override
	public void onGetReverseGeoCodeResult(ReverseGeoCodeResult result)
	{
		if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR)
		{

			tvCityName.setText("定位失败");
			return;
		}
		int starWith = 0;
		int endWith = 0;
		String address = result.getAddress();
		for (int i = 0; i < address.length(); i++)
		{
			char charAt = address.charAt(i);
			String cityName = String.valueOf(charAt);
			if ("省".equals(cityName))
			{
				starWith = i + 1;
			} else if ("市".equals(cityName))
			{
				endWith = i;
			}
		}
		cityName = address.substring(starWith, endWith);
		tvCityName.setText(cityName);
	}

	@Override
	protected void onStop()
	{
		mLocClient.stop();
		super.onStop();
	}

}
